package com.yyfax.infrastructure;

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.PermissionChecker;
import com.tbruyelle.rxpermissions.Permission;
import com.tbruyelle.rxpermissions.RxPermissions;
import rx.functions.Action1;

/**
 * Created by ying_he on 2017/6/15.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class PermissionsGrantedHelper {

    public static boolean checkAppPermissionWithVersion(Context context, String permission) {
        return isSdkUnderM() || checkAppPermission(context, permission);
    }

	public static boolean hasAllPermission(@NonNull Context context, @NonNull String... permissions) {
		if (isSdkUnderM()) return true;

		for (String permission : permissions) {
			boolean hasPermission = (ContextCompat.checkSelfPermission(context, permission) == PackageManager
					.PERMISSION_GRANTED);
			if (!hasPermission) return false;
		}
		return true;
	}

    public static boolean isSdkUnderM() {
        return Build.VERSION.SDK_INT < Build.VERSION_CODES.M;
    }

    private static boolean checkAppPermission(Context context, String permission) {
        boolean result = (PermissionChecker.checkSelfPermission(context, permission) == PermissionChecker.PERMISSION_GRANTED);//ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
        if (result) {
        } else {
        }
        return result;
    }

    /**
     * 同时请求多个权限，分别获取授权结果
     */
    public static void PermissionsEachGranted(final @NonNull RxPermissions rxPermissions, final PermissionsGrantedListener grantedListener, final @NonNull String... permissions) {
        rxPermissions.requestEach(permissions)
                .subscribe(new Action1<Permission>() {
                    @Override
                    public void call(Permission permission) {
                        grantedListener.permissionsEachGrantedResult(permission);

                    }
                });
    }

    /**
     * 同时请求多个权限,所有权限请求成功会返回true，若有一个权限未成功则返回false
     */
    public static void PermissionsAllGranted(final @NonNull RxPermissions rxPermissions, final PermissionsGrantedListener grantedListener, final int requestCode, final @NonNull String... permissions) {
        rxPermissions.request(permissions)
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean granted) {
                        grantedListener.PermissionsAllGrantedResult(granted, requestCode);
                    }
                });
    }

    public interface PermissionsGrantedListener{
        /**
         * 同时请求多个权限，分别获取授权结果
         * @param permission
         */
        void permissionsEachGrantedResult(Permission permission);
        /**
         * 同时请求多个权限,所有权限请求成功会返回true，若有一个权限未成功则返回false,
         * 通过requestCode判断是哪次权限请求返回的结果.
         */
        void PermissionsAllGrantedResult(boolean granted, int requestCode);
    }

    public static boolean checkCameraPermission() {
        boolean result = true;
        Camera mCamera = null;
        try {
            mCamera = Camera.open();
            // setParameters 是针对魅族做的。MX5 通过Camera.open() 拿到的Camera
            // 对象不为null
            Camera.Parameters mParameters = mCamera.getParameters();
            mCamera.setParameters(mParameters);
        } catch (Exception e) {
            result = false;
        } finally {
            if (mCamera != null) {
                mCamera.release();
                mCamera = null;
            }
        }
        return result;
    }
}
